;(function($,window,document){
    var userEmail = "",
        userMobile = "",
        userId = "",
        mobile = "",
        emailBox = $("#email-box"),
        telephoneBox = $("#telephone-box");
    //用户名检测
    userId = getQueryString('id');
    userEmail = getQueryString('userEmail');
    userMobile = getQueryString('userMobile');
    if(userEmail==='false'||userEmail===""){
        $("#email-box").hide();
    }
    if(userMobile==='false'||userMobile===""){
        $("#mobile-box").hide();
    }
    function checkUser(userName){
        if(userName===""){return '请输入您的用户名';}
        else{return true;}
    }
    // layer.tips('<i class="fa fa-minus-circle orange"></i>请输入您的登录名', '#userId',{time:false});
    //第一页的JS
    $("#choose").click(function(){
        if(checkUser($("#userId").val())===true){
            $.User.userExists($('#userId').val());
            if(typeof($.User.result.id)!='undefined'){
                userId = $.User.result.id;
                userMobile = $.User.result.exitMobile;
                userEmail = $.User.result.exitEmail;
                location.href = '/password/findWay?id='+userId+'&userMobile='+userMobile+'&userEmail='+userEmail;
            }else{
                $('#userError').text($.User.result);
            }
        }else{
            $('#userError').text('输入帐号不能为空');
        }
    });
    //第三页的JS
//      邮箱检测是否符合标准
    function checkEmail(str){
        var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
        if(re.test(str)){
            emailBox.find(".icon-success").show();
            emailBox.find(".icon-error").hide();
            $("#emailerr").hide();
            $("#email").removeClass("error");
        }else{
            emailBox.find(".icon-error").show();
            emailBox.find(".icon-success").hide();
            $("#emailerr").show();
            $("#email").addClass("error");
        }
    }
//      检测手机是否符合标准
    function checkMobile(str){
        var re = /^1\d{10}/;
        if(re.test(str)){
            telephoneBox.find(".icon-success").show();
            telephoneBox.find(".icon-error").hide();
            $("#telephoneerr").hide();
            $("#telephone").removeClass("error");
        }else{
            telephoneBox.find(".icon-error").show();
            telephoneBox.find(".icon-success").hide();
            $("#telephoneerr").show();
            $("#telephone").addClass("error");
        }
    }
//      密码强度检测
    function pwdStrong (str) {
        var lv = 0;
        if(str.match(/[a-z]/g)){lv++;}
        if(str.match(/[0-9]/g)){lv++;}
        if(str.match(/(.[^a-z0-9])/g)){lv++;}
        if(lv > 3){lv=3;}
        return lv;
    }
//      第二步转第三步
    $("#goToMobile").click(function(){
        location.href = "/password/mobile/choose?accountId="+userId;
    });
    $("#goToEmail").click(function(){
        location.href = "/password/email/choose?accountId="+userId;
    });
    $('#toEmail').click(function(){
        if(getQueryString('accountId')!==''&&getQueryString('accountId')!==null&&getQueryString('accountId')!=='undefined'){
            location.href = "/password/email/choose?accountId="+getQueryString('accountId');
        }
    });
    //获取短信验证码
    $("#getMobileCode").click(function(){
        $.User.sendMobileCode($("#userMobile").attr('value'));
        if($.User.flag === true){
            var time = 60;
            $("#getMobileCode").attr("disabled","disabled");
            countdown(time);
        }
    });
    //按钮计时器
    function countdown(time) {
        var timer = setTimeout(function() {
            countdown(time);
        },1000);
        var val = document.getElementById('getMobileCode');
        if (time === 0) {
            val.removeAttribute("disabled");
            val.innerText="重新发送";
            clearTimeout(timer);
        } else {
            val.setAttribute("disabled", true);
            val.innerText="重新发送(" + time + ")";
            time--;
        }
    }
    //检测用户手机和验证码是否匹配
    function checkMobileCode(code){
        if(code===""){
            return '请输入验证码';
        }else{
            $.User.checkMobileCode(userMobile,code);
            if($.User.flag===true){
                return true;
            }else{
                return '您输入的验证码有误';
            }
        }
    }
    $("#setPwdByMobile").click(function(){
        userMobile = $("#userMobile").attr('value');
        var code = $("#mobileCode").val();
        var result = checkMobileCode(code);
        var accountId = getQueryString('accountId');
        if(result === true){
            location.href = "/password/updatePage?id="+accountId+"&mobile="+userMobile+'&activationCode='+code;
        }else{
            $("#codeError").text('验证码不正确');
        }
    });
    $("#mobileCode").blur(function(){
        userMobile = $("#userMobile").attr('value');
        var code = $("#mobileCode").val();
        var result = checkMobileCode(code);
        if(result === true){
            $("#codeError").text('');
        }else{
            $("#codeError").text('验证码不正确');
        }
    });
    $("#userName").text(getQueryString('name'));
//    修改密码
    $("#updatePwd").click(function(){
        var url = "/password/modify_by_mobile";
        var password = $('#pwd').val();
        var result = checkPwd(password);
        if(result=== true){
            var confirm = eqPwd();
            $('#pwdbox').find('p:eq(0)').text('');
            if(confirm===true){
                var postData = {
                    id:$("#id").val(),
                    newPassword:$("#pwdRepeat").val(),
                    activationCode: getQueryString("activationCode"),
                    mobile : getQueryString("mobile")
                };
                if(postData.mobile == null){
                    url = "/password/modify_by_email";
                }
                $.ajax({
                    url:url,
                    data:postData,
                    type:'post',
                    success:function(data){
                        if(data.result===true){
                            location.href = "/password/success";
                        }else{
                            $('#pwdRepeat-box').find('p:eq(0)').text(data.result);
                        }
                    }
                });
            }
        }else{
            $('#pwdbox').find('p:eq(0)').text(result);
        }
    });
    //进行密码校验
    function checkPwd(str){
        var re = /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,20}$/;
        if(str.length === 0){return '密码不能为空';}
        else if(str.length<6){return '密码太短，至少6个字符';}
        else if(str.length>20){return '密码过长,不能超过20个字符';}
        else if(re.test(str)){return true;}
        else{return '密码格式不正确';}
    }
    //密码二次校验
    function eqPwd(){
        var oldPwd = $('#pwd').val();
        var newPwd = $("#pwdRepeat").val();
        var _this = $('#pwdRepeat-box p:eq(0)');
        if(oldPwd==newPwd){_this.text(''); return true;}
        else if(newPwd.length === 0){_this.text('密码不能为空');return false;}
        else if(newPwd.length>20){_this.text('密码过长,不能超过20个字符');return false;}
        else if(newPwd.length<6){_this.text('密码太短，至少6个字符');return false;}
        else{_this.text('两次密码输入不一致');return false;}
    }
//      输入时检测密码强度
    $("#pwd").on("input propertychange",function(){
        var weight = pwdStrong($(this).val());
        $("#pwdstrength b").removeClass("active");
        for (var i = 0; i < weight; i++) {
            $("#pwdstrength b:eq("+i+")").addClass("active");
        }
    });
    $("#gotoHome").click(function(){
        location.href = '/login/index';
    });
    //获取地址栏值
    function getQueryString(name){
         var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
         var r = window.location.search.substr(1).match(reg);
         if(r!=null){
             return  encodeURI(r[2]);
         }
        return null;
    }
})(jQuery,window,document);
